﻿using NumericalAnalysis.DataInterpolation;
using ScottPlot.Avalonia;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using MathNet.Numerics.Interpolation;
using System.Diagnostics;

namespace NumericalAnalysis.Display.Mission
{
    internal class InterpolationMission
    {


        public static void LagrangeInterpolationDisplay(AvaPlot avaPlot1)
        {
            var lagrangeInterpolationTest = new LagrangeInterpolationTest();
            var pnts = lagrangeInterpolationTest.ExampleSin();

            avaPlot1.Plot.AddScatter(
                pnts.Item1.Select(p => p.Item1).ToArray(),
                pnts.Item1.Select(p => p.Item2).ToArray()
               );
            avaPlot1.Plot.AddScatter(
                pnts.Item2.Select(p => p.Item1).ToArray(),
                pnts.Item2.Select(p => p.Item2).ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            //double[] dataX = new double[] { 1, 2, 3, 4, 5 };
            //double[] dataY = new double[] { 1, 4, 9, 16, 25 };
            //avaPlot1.Plot.AddScatter(dataX, dataY);
            //avaPlot1.Refresh(); 
            //avaPlot1.Plot.XLabel("时间");
            //avaPlot1.Plot.YLabel("数据");
            //avaPlot1.Plot.Title("测试曲线");
            //List<double> datetime = new List<double>();
            //List<double> value1 = new List<double>();
            //List<double> value2 = new List<double>();
            //List<double> value3 = new List<double>();
            //Random random = new Random();
            //datetime.Add(DateTime.Now.ToOADate());
            //value1.Add(random.NextDouble() * 10);
            //value2.Add(random.NextDouble() * 30);
            //value3.Add(random.NextDouble() * 50);

            //avaPlot1.Plot.AxisAuto();
            //avaPlot1.Plot.AddSignalXY(datetime.ToArray(), value1.ToArray(), Color.Blue);
            //avaPlot1.Plot.AddSignalXY(datetime.ToArray(), value2.ToArray(), Color.Red);
            //avaPlot1.Plot.AddSignalXY(datetime.ToArray(), value3.ToArray(), Color.Yellow);

            //avaPlot1.Refresh();

            Thread.Sleep(1000);

        }

        public static void AitkenInterpolationDisplay(AvaPlot avaPlot1)
        {
            var pnts = AitkenInterpolationTest.ExampleSin();

            avaPlot1.Plot.AddScatter(
                pnts.Item1.Select(p => p.Item1).ToArray(),
                pnts.Item1.Select(p => p.Item2).ToArray()
               );
            avaPlot1.Plot.AddScatter(
                pnts.Item2.Select(p => p.Item1).ToArray(),
                pnts.Item2.Select(p => p.Item2).ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            Thread.Sleep(1000);

        }


        public static void NewtonDifferenceQuotientDisplay(AvaPlot avaPlot1)
        {
            var pnts = NewtonDifferenceQuotientTest.ExampleSin();

            avaPlot1.Plot.AddScatter(
                pnts.Item1.Select(p => p.Item1).ToArray(),
                pnts.Item1.Select(p => p.Item2).ToArray()
               );
            avaPlot1.Plot.AddScatter(
                pnts.Item2.Select(p => p.Item1).ToArray(),
                pnts.Item2.Select(p => p.Item2).ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            Thread.Sleep(100);
        }

        public static void NewtonDifferenceInterpolationDisplay(AvaPlot avaPlot1)
        {
            var newtonDifferenceInterpolationTestDemo
                = new NewtonDifferenceInterpolationTestDemo();
            var pnts = newtonDifferenceInterpolationTestDemo.ExampleRunge();
            //pnts = NewtonDifferenceInterpolationTest .ExampleRunge();

            avaPlot1.Plot.AddScatter(
                pnts.Item1.Select(p => p.Item1).ToArray(),
                pnts.Item1.Select(p => p.Item2).ToArray()
               );
            avaPlot1.Plot.AddScatter(
                pnts.Item2.Select(p => p.Item1).ToArray(),
                pnts.Item2.Select(p => p.Item2).ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            Thread.Sleep(100);
        }

        public static void HermiteInterpolationDisplay(AvaPlot avaPlot1)
        {

            HermiteInterpolationTest hermiteInterpolationTest = new HermiteInterpolationTest();
            var pnts = hermiteInterpolationTest.ExampleSin();

            avaPlot1.Plot.AddScatter(
                pnts.Item1.Select(p => p.Item1).ToArray(),
                pnts.Item1.Select(p => p.Item2).ToArray()
               );
            avaPlot1.Plot.AddScatter(
                pnts.Item2.Select(p => p.Item1).ToArray(),
                pnts.Item2.Select(p => p.Item2).ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            Thread.Sleep(100);
        }

        public static void MathNetInterpDisplay(AvaPlot avaPlot1)
        {
            HermiteInterpolationTest hermiteInterpolationTest = new HermiteInterpolationTest();
            var pnts = hermiteInterpolationTest.ExampleSin();

            var xdata = pnts.Item1.Select(p => p.Item1).ToArray();
            var ydata = pnts.Item1.Select(p => p.Item2).ToArray();

            avaPlot1.Plot.AddScatter(
                xdata,
                ydata
              );

            var spline = CubicSpline.InterpolateAkimaSorted(xdata, ydata);
            // var spline = CubicSpline.InterpolateNaturalSorted(xdata, ydata);

            var xs = NumericalService.LinSpace(xdata.Min(), xdata.Max(), 0.05);
            var ys = xs.Select(x => spline.Interpolate(x));

            avaPlot1.Plot.AddScatter(
                xs.ToArray(),
                ys.ToArray());
            avaPlot1.Refresh();

            avaPlot1.Plot.XLabel("X");
            avaPlot1.Plot.YLabel("Y");
            avaPlot1.Plot.Title("测试曲线");

            avaPlot1.Plot.AxisAuto();

            avaPlot1.Refresh();

            Thread.Sleep(100);
        }





    }
}
